{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['211.xls', '212.xls', '213.xls', '214.xls', '215.xls', '216.xls']\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "folder_path = Path('C:\\\\Users\\\\Administrator\\Desktop\\\\计算机表')\n",
    "file_list = folder_path.glob('*.xlsx*')\n",
    "lists = []\n",
    "for i in file_list:\n",
    "    file_name = i.name\n",
    "    lists.append(file_name)\n",
    "print(lists)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlwings as xw\n",
    "app = xw.App(visible=False,add_book=False)\n",
    "workbook = app. books.add()\n",
    "workbook.save('C:\\\\Users\\\\Administrator\\Desktop\\\\计算机表\\\\211.xls')\n",
    "workbook.close()\n",
    "app.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlwings as xw\n",
    "import os\n",
    "\n",
    "# 设置工作簿保存的目录\n",
    "save_dir = 'C:\\\\Users\\\\Administrator\\Desktop\\\\计算机表'\n",
    "\n",
    "# 确保目录存在\n",
    "if not os.path.exists(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "\n",
    "# 启动Excel应用\n",
    "app = xw.App(visible=False, add_book=False)\n",
    "\n",
    "# 批量创建和保存工作簿\n",
    "for i in range(1, 11):  # 创建10个工作簿\n",
    "    workbook = app.books.add()\n",
    "    # 构建保存路径和文件名\n",
    "    save_path = os.path.join(save_dir, f'21{i}.xls')\n",
    "    workbook.save(save_path)\n",
    "    workbook.close()\n",
    "\n",
    "# 关闭Excel应用\n",
    "app.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Book [211.xls]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import xlwings as xw\n",
    "app = xw.App(visible=True,add_book=False)\n",
    "file_path='C:\\\\Users\\\\Administrator\\Desktop\\\\计算机表\\\\211.xls'\n",
    "app.books.open(file_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已打开: 211.xls\n",
      "已打开: 212.xls\n",
      "已打开: 213.xls\n",
      "已打开: 214.xls\n",
      "已打开: 215.xls\n",
      "已打开: 216.xls\n",
      "已打开: 217.xls\n",
      "已打开: 218.xls\n",
      "已打开: 219.xls\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import xlwings as xw\n",
    "\n",
    "# 设置工作簿所在的目录\n",
    "workbook_dir = 'C:\\\\Users\\\\Administrator\\\\Desktop\\\\计算机表'\n",
    "\n",
    "# 确保目录存在\n",
    "if not os.path.exists(workbook_dir):\n",
    "    print(\"目录不存在\")\n",
    "    exit()\n",
    "\n",
    "# 获取目录下的所有文件\n",
    "files = os.listdir(workbook_dir)\n",
    "\n",
    "# 启动Excel应用\n",
    "app = xw.App(visible=True, add_book=False)\n",
    "\n",
    "# 遍历文件，打开工作簿\n",
    "for file in files:\n",
    "    # 检查文件扩展名是否为.xls或.xlsx\n",
    "    if file.endswith('.xls') or file.endswith('.xlsx'):\n",
    "        # 构建完整的文件路径\n",
    "        file_path = os.path.join(workbook_dir, file)\n",
    "        # 打开工作簿\n",
    "        workbook = app.books.open(file_path)\n",
    "        print(f\"已打开: {file}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已重命名: 211.xls -> 201.xls\n",
      "已重命名: 212.xls -> 202.xls\n",
      "已重命名: 213.xls -> 203.xls\n",
      "已重命名: 214.xls -> 204.xls\n",
      "已重命名: 215.xls -> 205.xls\n",
      "已重命名: 216.xls -> 206.xls\n",
      "已重命名: 217.xls -> 207.xls\n",
      "已重命名: 218.xls -> 208.xls\n",
      "已重命名: 219.xls -> 209.xls\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# 设置工作簿所在的目录\n",
    "workbook_dir = 'C:\\\\Users\\\\Administrator\\\\Desktop\\\\计算机表'\n",
    "\n",
    "# 确保目录存在\n",
    "if not os.path.exists(workbook_dir):\n",
    "    print(\"目录不存在\")\n",
    "    exit()\n",
    "\n",
    "# 获取目录下的所有文件\n",
    "files = os.listdir(workbook_dir)\n",
    "\n",
    "# 重命名文件\n",
    "for index, file in enumerate(files, start=1):\n",
    "    # 检查文件扩展名是否为.xls或.xlsx\n",
    "    if file.endswith('.xls') or file.endswith('.xlsx'):\n",
    "        # 构建原文件路径\n",
    "        old_file_path = os.path.join(workbook_dir, file)\n",
    "        # 构建新文件路径\n",
    "        new_file_name = f'20{index}.xls'\n",
    "        new_file_path = os.path.join(workbook_dir, new_file_name)\n",
    "        # 重命名文件\n",
    "        os.rename(old_file_path, new_file_path)\n",
    "        print(f\"已重命名: {file} -> {new_file_name}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import xlwings as xw\n",
    "\n",
    "# 设置工作簿所在的目录\n",
    "workbook_dir = 'C:\\\\Users\\\\Administrator\\\\Desktop\\\\新能源汽车备案信息\\\\'\n",
    "workbook_name = '新能源汽车备案信息.xlsx'\n",
    "workbook_path = os.path.join(workbook_dir, workbook_name)\n",
    "\n",
    "# 确保工作簿存在\n",
    "if not os.path.exists(workbook_path):\n",
    "    print(\"工作簿不存在\")\n",
    "    exit()\n",
    "\n",
    "# 打开工作簿\n",
    "app = xw.App(visible=False)\n",
    "workbook = app.books.open(workbook_path)\n",
    "\n",
    "# 遍历工作簿中的每个工作表\n",
    "for sheet in workbook.sheets:\n",
    "    # 创建新的工作簿\n",
    "    new_workbook = app.books.add()\n",
    "    # 复制工作表到新的工作簿\n",
    "    sheet.api.Copy(Before=new_workbook.sheets[0].api)\n",
    "    # 删除新工作簿中的默认工作表\n",
    "    new_workbook.sheets[0].delete()\n",
    "    # 保存新工作簿\n",
    "    new_workbook.save(os.path.join(workbook_dir, f\"{sheet.name}.xlsx\"))\n",
    "    # 关闭新工作簿\n",
    "    new_workbook.close()\n",
    "\n",
    "# 关闭原始工作簿\n",
    "workbook.close()\n",
    "\n",
    "# 关闭Excel应用\n",
    "app.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import xlwings as xw\n",
    "\n",
    "# 设置工作簿所在的目录\n",
    "workbook_dir = 'C:\\\\Users\\\\Administrator\\\\Desktop\\\\新能源汽车备案信息\\\\拆分'\n",
    "\n",
    "# 确保目录存在\n",
    "if not os.path.exists(workbook_dir):\n",
    "    print(\"目录不存在\")\n",
    "    exit()\n",
    "\n",
    "# 获取目录下的所有工作簿文件\n",
    "files = [f for f in os.listdir(workbook_dir) if f.endswith(('.xls', '.xlsx'))]\n",
    "\n",
    "# 创建一个新的工作簿用于合并\n",
    "app = xw.App(visible=False)\n",
    "new_workbook = app.books.add()\n",
    "\n",
    "# 遍历所有工作簿并合并\n",
    "for file in files:\n",
    "    # 打开工作簿\n",
    "    workbook_path = os.path.join(workbook_dir, file)\n",
    "    workbook = app.books.open(workbook_path)\n",
    "    \n",
    "    # 遍历工作簿中的每个工作表并复制到新工作簿\n",
    "    for sheet in workbook.sheets:\n",
    "        sheet.api.Copy(Before=new_workbook.sheets[0].api)\n",
    "    \n",
    "    # 关闭原始工作簿\n",
    "    workbook.close()\n",
    "\n",
    "# 删除新工作簿中的默认工作表\n",
    "new_workbook.sheets[0].delete()\n",
    "\n",
    "# 保存合并后的工作簿\n",
    "new_workbook.save(os.path.join(workbook_dir, '合并.xlsx'))\n",
    "\n",
    "# 关闭新工作簿\n",
    "new_workbook.close()\n",
    "\n",
    "# 关闭Excel应用\n",
    "app.quit()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
